home *** CD-ROM | disk | FTP | other *** search
- 10 REM One-dimensional cellular automata
- 20 REM by Kenneth E. Perry
- 30 REM ---------------------------------------------------
- 40 REM Screen setup
- 50 KEY OFF
- 60 SCREEN 1,0 :REM Medium resolution (320 x 200)
- 70 COLOR 0,0 :REM Use color palette 0.
- 80 REM ---------------------------------------------------
- 90 REM Initialize variables
- 100 DEFINT A-Z :REM All variables are integers.
- 110 HRES=320 :REM Number of points in horizontally.
- 120 VRES=200 :REM Number of points vertically.
- 130 SF=2 :REM SF=1 for denser graph.
- 140 BY=10 :REM Offset to leave one line blank.
- 150 GN=INT(VRES/SF)-BY
- 160 MC=INT(HRES/SF) :REM Max no. of cells.
- 170 IF MC>255 THEN MC=255 :REM Prevent string overflow.
- 180 MI=40 :REM Characters/line in graphics mode.
- 190 BG=0 :REM Color of background.
- 200 NU$="" :REM No spaces inside quotes
- 210 DIM CL(3), A(MC-1), B(MC-1), RU(9), K$(-INT(-MC/MI))
- 220 REM Initialize color mapping
- 230 FOR J=0 TO 3: READ CL(J): NEXT J
- 240 DATA 0,2,3,1
- 250 REM --------------------------------------------------
- 260 REM Initialize the rule
- 270 R$="0000000000" :REM Ten zeroes
- 280 CLS
- 290 PRINT "Current rule is shown below. The rule"
- 300 PRINT "must contain exactly 10 digits, using"
- 310 PRINT "only 0, 1, 2, and 3."
- 320 PRINT "Move the cursor across the rule, make"
- 330 PRINT "changes as desired, and press <cr>."
- 340 PRINT: PRINT R$
- 350 LOCATE 7,1
- 360 LINE INPUT RT$
- 370 IF RT$=NU$ THEN END
- 380 IF LEN(RT$)<>10 THEN 280
- 390 OK=(1=1)
- 400 FOR J=1 TO 10 :REM Make sure the rule is valid.
- 410 RU(J-1)=VAL(MID$(RT$,J,1))
- 420 OK=(OK AND RU(J-1)>=0 AND RU(J-1)<=3)
- 430 NEXT J
- 440 IF NOT OK THEN 280 ELSE R$=RT$
- 450 REM --------------------------------------------------
- 460 REM Get the number of cells in each generation
- 470 PRINT USING "How many cells/line (### to ###)";MI,MC;
- 480 INPUT NC
- 490 IF NC<MI OR NC>MC THEN 470
- 500 NL=INT((NC-1)/MI)+1 :REM Lines needed to show 1st gen.
- 510 REM --------------------------------------------------
- 520 REM Get the initial state of the cellular automaton
- 530 PRINT
- 540 INPUT "Initial=predefined or random (P/R)";PR$
- 550 IF PR$<>NU$ AND INSTR(1,"Rr",PR$)>0 THEN GOSUB
- 900 ELSE GOSUB 970
- 560 A(0)=0: A(NC-1)=0 :REM Force boundaries to zero.
- 570 REM --------------------------------------------------
- 580 REM Start the cellular automaton running
- 590 CLS
- 600 PRINT "Rule ";R$ :REM Display rule
- 610 FOR J=0 TO 3
- 620 LOCATE 1,20+J*4
- 630 PRINT USING "#=";J
- 640 NX=168+J*32
- 650 LINE (NX,0)-STEP(7,7),CL(J),BF
- 660 NEXT J
- 670 FOR Y=0 TO GN-1
- 680 FOR X=0 TO NC-1
- 690 IF X=0 OR X=NC-1 THEN V=3 ELSE V=CL(A(X))
- 700 PSET (X*SF,Y*SF+BY),V :REM Display the point.
- 710 NEXT X
- 720 REM --------------------------------------------------
- 730 REM Compute new state of automaton
- 740 FOR X=1 TO NC-2 :REM Don't change boundary cells.
- 750 Z=A(X-1)+A(X)+A(X+1)
- 760 B(X)=RU(Z)
- 770 NEXT X
- 780 REM Copy new values to A() from B()
- 790 FOR X=1 TO NC-2: A(X)=B(X): NEXT X
- 800 IF INKEY$<>NU$ THEN Y=GN-1 :REM Exit if key pressed.
- 810 NEXT Y
- 820 WHILE INKEY$<>NU$: WEND :REM Clear keyboard buffer.
- 830 LOCATE 1,17: PRINT "Continue or quit (C/Q)?";
- 840 CQ$=INKEY$
- 850 IF CQ$=NU$ THEN 840
- 860 WH=INT((INSTR(1,"CcQq",CQ$)+1)/2)+1 :REM wh = 1,2,3.
- 870 ON WH GOTO 840, 590, 280
- 880 REM --------------------------------------------------
- 890 REM Random initialization of automaton.
- 900 RANDOMIZE
- 910 FOR J=0 TO NC-1
- 920 A(J)=INT(RND*4) :REM Random values from 0 to 3
- 930 NEXT J
- 940 RETURN
- 950 REM --------------------------------------------------
- 960 REM Initialize array A() with a preset pattern
- 970 FOR J=1 TO NL
- 980 IF NC>=J*MI THEN LL=MI ELSE LL=NC-(J-1)*MI
- 990 K$(J)=STRING$(LL,".")
- 1000 CLS
- 1010 PRINT "Rule is: "; R$
- 1020 PRINT USING "Cells in range ### to ###:";
- (J-1)*MI+1, (J-1)*MI+LL
- 1030 PRINT "Move cursor across the field and make"
- 1040 PRINT "changes as desired. Then press <cr>."
- 1050 PRINT K$(J)
- 1060 LOCATE 5,1
- 1070 LINE INPUT K$(J)
- 1080 NEXT J
- 1090 K$=K$(1)+K$(2)+K$(3)+K$(4)
- 1100 REM Convert string to array values.
- 1110 FOR J=1 TO NC
- 1120 KK$=MID$(K$,J,1)
- 1130 IF INSTR(1,"0123",KK$)=0 THEN A(J-1)=BG ELSE
- A(J-1)= VAL(KK$)
- 1140 NEXT J
- 1150 RETURN